package com.swf.common.jar;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

import com.swf.common.base.*;
import com.swf.common.exception.*;
import com.swf.common.filter.*;
import com.swf.common.utils.*;

public class ClassInfoCenter {
	private  Map<String, ClassInfo> classInfoMap ;

	private ClassInfoCenter() {
	}
	 
	/**
	 * 解析文件，采用默认编码
	 * @param filePath
	 * @throws FwsException
	 */
	public ClassInfoCenter(String filePath) throws FwsException{
		ClassInfoXMLReader reader = new ClassInfoXMLReader(filePath);
		this.classInfoMap = reader.getResources();
	}
	/**解析文件，采用指定编码encoding
	 * @param filePath
	 * @param encoding
	 * @throws FwsException
	 */
	public ClassInfoCenter(String filePath,String encoding) throws FwsException{
		ClassInfoXMLReader reader = new ClassInfoXMLReader(filePath,encoding);
		this.classInfoMap = reader.getResources();
	}

	/**
	 * 获取classInfoMap
	 * @return
	 */
	public Map getClassInfoMap() {
		return classInfoMap;
	}
	

	/**
	 * 获取classInfo
	 * @param classInfoId
	 * @return
	 */
	public ClassInfo getClassInfo(String classInfoId) {
		return classInfoMap.get(classInfoId);
	}
	
	/**
	 * 获取指定classInfo下的包含所有jar的map
	 * @param classInfoId
	 * @return
	 */
	public Map getJarsUnderClassInfo(String classInfoId){
		return classInfoMap.get(classInfoId).getJarMap();
	}
	
	/**
	 * 获取指定classInfo下的名称为jarName的jar
	 * @param classInfoId
	 * @param jarName
	 * @return
	 */
	public Jar getJarUnderClassInfo(String classInfoId,String jarName){
		return classInfoMap.get(classInfoId).getJar(jarName);
	}
	
	public static void copyByConfig(String classInfoId,String cfgfile) throws IOException{
		Map jarmap = new HashMap();
		ClassInfoCenter center = null;
		ClassInfo classInfo = null;
		try {
			center = new ClassInfoCenter(cfgfile);
			classInfo = center.getClassInfo(classInfoId);
			jarmap = classInfo.getJarMap();
		} catch (FwsException e) {
			e.printStackTrace();
		}
		final String[] strs = FwsCollectionUtils.getStringMapKeys(jarmap);
		String from = classInfo.getFrom();
		String to = classInfo.getTo();
		File destFile = FwsFileUtils.getFile(to);
		File srcFile= FwsFileUtils.getFile(from);
		FwsFileUtils.copyFileToDir(srcFile, destFile, new FwsNameFilter(){
			@Override
			public boolean include(String name) {
				return FwsStringUtils.contains(strs, name);
			}
		});
	}
	
	public String toEscapeString(int tabCount) {
		String tab = FwsStringUtils.repeat(" ", tabCount);
		StringBuffer sb = new StringBuffer();
		sb.append(FwsStringUtils.addBefore("<?xml version=\"1.0\" encoding=\"UTF-8\"?>",tab));
		sb.append(FwsConstants.line_separator);
		sb.append(FwsStringUtils.addBefore("<classInfos>",tab));
		sb.append(FwsConstants.line_separator);
		Iterator it = classInfoMap.entrySet().iterator();
		while (it.hasNext()){
			Entry entry = (Entry) it.next();
			ClassInfo classInfo = (ClassInfo) entry.getValue();
			sb.append(FwsStringUtils.addBefore(classInfo.toEscapeString(4),tab));
			sb.append(FwsConstants.line_separator);
		}
		sb.append(FwsStringUtils.addBefore("</classInfos>",tab));
		return sb.toString();
	}
	
	public String toEscapeString() {
		return toEscapeString(0);
	}
	
	@Override
	public String toString() {
		return FwsStringEscapeUtils.unescapeXml(toEscapeString());
	}
	
	public String toString(int tabCount) {
		return FwsStringEscapeUtils.unescapeXml(toEscapeString(tabCount));
	}
	
}
